package stbm.gui;

import java.awt.*;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import java.awt.event.*;

import stbm.guihelpers.*;
import stbm.objects.*;

/**
 * The class representing the startup screen
 * 
 * @author Nirav B.
 * @author Nisha Lad
 * 
 */
public class GameInitPanel extends BasicPanel {

	private JLabel lblWelcomeMessage;

	private JLabel lblPilotPoints;

	private JLabel lblEngineerPoints;

	private JLabel lblTraderPoints;

	private JLabel lblFighterPoints;

	private JTextField txtPlayerName;

	private JTextField txtPilotPoints;

	private JTextField txtEngineerPoints;

	private JTextField txtTraderPoints;

	private JTextField txtFighterPoints;

	private JLabel lblInstructions;

	private JLabel lblLevel;

	private JButton start;

	private JRadioButton lvlBeginner, lvlEasy, lvlNormal, lvlHard,
			lvlImpossible;

	private Image img;

	private static boolean setDifficulty = false;

	/**
	 * Creates a new GameInitPanel
	 */
	public GameInitPanel(GameManager gameManager) {

		super(gameManager);
		img = new ImageIcon("images/TitleBackgroundNew.png").getImage();
		this.setLayout(new GridBagLayout());
		GridBagConstraints mainPanelConstraints = new GridBagConstraints();

		JPanel enterNamePanel = new JPanel(new GridBagLayout());
		GridBagConstraints entNameConstraints = new GridBagConstraints();

		JPanel pointSelectionPanel = new JPanel(new GridBagLayout());
		GridBagConstraints pointsConstraints = new GridBagConstraints();

		JPanel levelSelectionPanel = new JPanel(new GridBagLayout());
		GridBagConstraints levelConstraints = new GridBagConstraints();
		levelSelectionPanel.setOpaque(false);

		lblWelcomeMessage = new JLabel(
				"<html><h1>Welcome to SpaceTrader!</h1></html>");
		lblWelcomeMessage.setForeground(Color.white);
		lblInstructions = new JLabel(
				"<html><br/><p>You have 16 skill points to start off with. <br/>Please distribute your points<br/> to the skills listed below:</p><br/></html>");
		lblInstructions.setForeground(Color.white);
		lblPilotPoints = new JLabel("Pilot Points: ");
		lblEngineerPoints = new JLabel("Engineer Points: ");
		lblTraderPoints = new JLabel("Trader Points: ");
		lblFighterPoints = new JLabel("Fighter Points: ");
		lblPilotPoints.setForeground(Color.white);
		lblEngineerPoints.setForeground(Color.white);
		lblTraderPoints.setForeground(Color.white);
		lblFighterPoints.setForeground(Color.white);
		txtPlayerName = new JTextField(GameSpecifics.DEFAULT_TEXTBOX_WIDTH);
		txtPlayerName.setText("Enter Player Name");
		txtPilotPoints = new JTextField(GameSpecifics.SMALL_TEXTBOX_WIDTH);
		txtEngineerPoints = new JTextField(GameSpecifics.SMALL_TEXTBOX_WIDTH);
		txtTraderPoints = new JTextField(GameSpecifics.SMALL_TEXTBOX_WIDTH);
		txtFighterPoints = new JTextField(GameSpecifics.SMALL_TEXTBOX_WIDTH);

		entNameConstraints.gridx = 0;
		entNameConstraints.gridy = 0;
		enterNamePanel.add(lblWelcomeMessage, entNameConstraints);

		entNameConstraints.gridx = 0;
		entNameConstraints.gridy = 2;
		enterNamePanel.add(txtPlayerName, entNameConstraints);

		entNameConstraints.gridx = 0;
		entNameConstraints.gridy = 3;
		enterNamePanel.add(lblInstructions, entNameConstraints);

		pointsConstraints.gridx = 0;
		pointsConstraints.gridy = 0;
		pointSelectionPanel.add(lblPilotPoints, pointsConstraints);
		pointsConstraints.gridx = 1;
		pointsConstraints.gridy = 0;
		pointSelectionPanel.add(txtPilotPoints, pointsConstraints);
		pointsConstraints.gridx = 0;
		pointsConstraints.gridy = 1;
		pointSelectionPanel.add(lblEngineerPoints, pointsConstraints);
		pointsConstraints.gridx = 1;
		pointsConstraints.gridy = 1;
		pointSelectionPanel.add(txtEngineerPoints, pointsConstraints);
		pointsConstraints.gridx = 0;
		pointsConstraints.gridy = 2;
		pointSelectionPanel.add(lblTraderPoints, pointsConstraints);
		pointsConstraints.gridx = 1;
		pointsConstraints.gridy = 2;
		pointSelectionPanel.add(txtTraderPoints, pointsConstraints);
		pointsConstraints.gridx = 0;
		pointsConstraints.gridy = 3;
		pointSelectionPanel.add(lblFighterPoints, pointsConstraints);
		pointsConstraints.gridx = 1;
		pointsConstraints.gridy = 3;
		pointSelectionPanel.add(txtFighterPoints, pointsConstraints);

		lblLevel = new JLabel("<html><br/>Please select a level:<br/></html>");
		lblLevel.setForeground(Color.white);
		lvlBeginner = new JRadioButton("Beginner");
		lvlBeginner.setOpaque(false);
		lvlBeginner.setForeground(Color.yellow);
		lvlEasy = new JRadioButton("Easy");
		lvlEasy.setOpaque(false);
		lvlEasy.setForeground(Color.yellow);
		lvlNormal = new JRadioButton("Normal");
		lvlNormal.setOpaque(false);
		lvlNormal.setForeground(Color.yellow);
		lvlHard = new JRadioButton("Hard");
		lvlHard.setOpaque(false);
		lvlHard.setForeground(Color.yellow);
		lvlImpossible = new JRadioButton("Impossible");
		lvlImpossible.setOpaque(false);
		lvlImpossible.setForeground(Color.yellow);
		ButtonGroup bg = new ButtonGroup();
		bg.add(lvlBeginner);
		bg.add(lvlEasy);
		bg.add(lvlNormal);
		bg.add(lvlHard);
		bg.add(lvlImpossible);
		lvlBeginner.addActionListener(new DifficultyListener());
		lvlEasy.addActionListener(new DifficultyListener());
		lvlNormal.addActionListener(new DifficultyListener());
		lvlHard.addActionListener(new DifficultyListener());
		lvlImpossible.addActionListener(new DifficultyListener());

		levelConstraints.gridx = 0;
		levelConstraints.gridy = 0;
		levelSelectionPanel.add(lblLevel, levelConstraints);

		levelConstraints.gridx = 0;
		levelConstraints.gridy = 1;
		levelSelectionPanel.add(lvlBeginner, levelConstraints);

		levelConstraints.gridx = 0;
		levelConstraints.gridy = 2;
		levelSelectionPanel.add(lvlEasy, levelConstraints);

		levelConstraints.gridx = 0;
		levelConstraints.gridy = 3;
		levelSelectionPanel.add(lvlNormal, levelConstraints);

		levelConstraints.gridx = 0;
		levelConstraints.gridy = 4;
		levelSelectionPanel.add(lvlHard, levelConstraints);

		levelConstraints.gridx = 0;
		levelConstraints.gridy = 5;
		levelSelectionPanel.add(lvlImpossible, levelConstraints);

		start = new JButton("Begin");
		start.addActionListener(new ClickListener());

		mainPanelConstraints.gridx = 0;
		mainPanelConstraints.gridy = 0;
		enterNamePanel.setOpaque(false);
		add(enterNamePanel, mainPanelConstraints);

		mainPanelConstraints.gridx = 0;
		mainPanelConstraints.gridy = 1;
		pointSelectionPanel.setOpaque(false);
		add(pointSelectionPanel, mainPanelConstraints);

		mainPanelConstraints.gridx = 0;
		mainPanelConstraints.gridy = 2;
		levelSelectionPanel.setOpaque(false);
		add(levelSelectionPanel, mainPanelConstraints);

		mainPanelConstraints.gridx = 0;
		mainPanelConstraints.gridy = 3;
		mainPanelConstraints.insets = new Insets(20, 20, 20, 20);
		add(start, mainPanelConstraints);

	}

	public void paintComponent(Graphics g) {
		g.drawImage(img, 0, 0, null);
	}

	public void hidePanel() {
		this.setVisible(false);

	}

	public class DifficultyListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if (source == lvlBeginner || source == lvlEasy
					|| source == lvlNormal || source == lvlHard
					|| source == lvlImpossible) {
				setDifficulty = true;
			}
		}
	}

	public class ClickListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			Object source = e.getSource();
			if (source == start) {

				if (!txtPilotPoints.getText().equals("")
						&& !txtEngineerPoints.getText().equals("")
						&& !txtTraderPoints.getText().equals("")
						&& !txtFighterPoints.getText().equals("")) {
					String userName = txtPlayerName.getText();
					int pt1 = Integer.parseInt(txtPilotPoints.getText());
					int pt2 = Integer.parseInt(txtEngineerPoints.getText());
					int pt3 = Integer.parseInt(txtTraderPoints.getText());
					int pt4 = Integer.parseInt(txtFighterPoints.getText());
					if (Validation.gameInitValidates(userName, pt1, pt2, pt3,
							pt4, setDifficulty)) {
						Player player = new Player(userName, pt1, pt2, pt3, pt4);
						System.out.println("Welcome " + player.getName() + "!");
						Universe universe = new Universe();
						System.out.println("UNIVERSE " + universe.test());
						Ship ship = new Ship();
						gameManager.setUniverse(universe);
						gameManager.setPlayer(player);
						gameManager.setShip(ship);
						gameManager.gamePanel = new GamePanel(gameManager);
						try {
							gameManager.saveState();
						} catch (ParserConfigurationException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (TransformerException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						gameManager.switchScreens(GameSpecifics.GAME_SCREEN);

					}

				} else {
					JOptionPane
							.showMessageDialog(
									null,
									"Please enter a point for each of the skill levels...",
									"ERROR", JOptionPane.ERROR_MESSAGE);

				}

			}

		}

	}

}
